//写一个泛型类  当中有个方法   可以求数组最大值
class Alg<E extends Comparable<E>>{

    public E findMax(E[] array){
        E max = array[0];
        for (int i = 0; i < array.length; i++) {
            //if(max < array[i]){
            if(max.compareTo(array[i])<0){
                max = array[i];
            }
        }
        return max;
    }
}

class Alg2{
    public<E extends Comparable<E>> E findMax(E[] array){
        E max = array[0];
        for (int i = 0; i < array.length; i++) {
            //if(max < array[i]){
            if(max.compareTo(array[i])<0){
                max = array[i];
            }
        }
        return max;
    }
}

class Alg3{
    public static <E extends Comparable<E>> E findMax(E[] array){
        E max = array[0];
        for (int i = 0; i < array.length; i++) {
            //if(max < array[i]){
            if(max.compareTo(array[i])<0){
                max = array[i];
            }
        }
        return max;
    }
}
public class TestGeneric {
    public static void main(String[] args) {
        Integer[] array = {1,2,3,4,45,5,6,8};
        int ret = Alg3.findMax(array);
        System.out.println(ret);
    }
    public static void main2(String[] args) {
        Alg2 alg2 = new Alg2();
        Integer[] array = {1,2,3,4,45,5,6,8};
        //它是怎么知道你传的是什么类型的呢？
        int ret = alg2.findMax(array);//答：它会根据你传入的array的类型判断
        //int ret = alg2.<Integer>findMax(array);
        System.out.println(ret);
    }
    public static void main1(String[] args) {
        Integer[] array = {1,2,3,4,45,5,6,8};
        Alg<Integer> alg = new Alg<>();
        Integer a = alg.findMax(array);
        System.out.println(a);
    }
}
